package com.aigou.admin.aigouspringboot.service.impl;

import com.aigou.admin.aigouspringboot.common.Constants;
import com.aigou.admin.aigouspringboot.entity.Product;
import com.aigou.admin.aigouspringboot.entity.ReceivingAddress;
import com.aigou.admin.aigouspringboot.entity.ShopCart;
import com.aigou.admin.aigouspringboot.entity.ShopCartProduct;
import com.aigou.admin.aigouspringboot.entity.dto.ShopCartProductDTO;
import com.aigou.admin.aigouspringboot.mapper.ShopCartMapper;
import com.aigou.admin.aigouspringboot.mapper.ShopCartProductMapper;
import com.aigou.admin.aigouspringboot.service.ShopCartService;
import com.aigou.admin.aigouspringboot.utils.RandomUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ShopCartServiceImpl implements ShopCartService {

    @Resource
    private ShopCartMapper shopCartMapper;

    @Resource
    private ShopCartProductMapper shopCartProductMapper;


    @Override
    public List<ShopCart> selectAll() {
        return shopCartMapper.selectAll();
    }

    @Override
    public PageInfo<ShopCart> selectShopCartListByPage(Integer current) {
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        List<ShopCart> shopCartList = shopCartMapper.selectAll();
        return new PageInfo<>(shopCartList);
    }

    @Override
    public int deleteById(String id) {
        return shopCartMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ShopCart selectById(String id) {
        return shopCartMapper.selectByPrimaryKey(id);
    }

    @Override
    public int edit(ShopCart shopCart) {
        return shopCartMapper.updateByPrimaryKeySelective(shopCart);
    }

    @Override
    public int save(ShopCart shopCart) {
        return shopCartMapper.insertSelective(shopCart);
    }

    @Override
    public List<ReceivingAddress> getAddress(String userId) {
        return shopCartMapper.getAddressByUserId(userId);
    }

    @Override
    public List<Product> getProduct(String userId) {
        return shopCartMapper.gerProductByUserId(userId);
    }

    @Override
    public int addProductToCart(ShopCartProductDTO shopCartProductDTO) {
        ShopCart shopCart = shopCartMapper.selectCartByUserId(shopCartProductDTO.getUserId());
        if (shopCart == null) {
            // 如果没有购物车，不进行任何操作
            return 0;
        }
        // 检查购物车中是否已有该商品
        ShopCartProduct shopCartProduct = shopCartProductMapper.selectProductByCartIdAndProductId(shopCart.getId(), shopCartProductDTO.getProductId());
        if (shopCartProduct != null) {
            // 如果商品已存在，增加商品数量
            int updatedRows = shopCartProductMapper.updateProductNum(shopCartProduct.getId(), shopCartProduct.getProductNum() + shopCartProductDTO.getProductNum());
            return updatedRows;
        }
            // 如果商品不存在，添加新商品
        ShopCartProduct addShopCartProduct = new ShopCartProduct();
        addShopCartProduct.setId(RandomUtil.getRandomNumber());
        addShopCartProduct.setShopCartId(shopCart.getId());
        addShopCartProduct.setProductId(shopCartProductDTO.getProductId());
        addShopCartProduct.setProductNum(shopCartProductDTO.getProductNum());

        return shopCartProductMapper.insertSelective(addShopCartProduct);

    }
}



